
const MenuServices = require("../services/MenuServices")
const MenuSchema = require("../schema/MenuSchema")
const { validate, errorResponese, successResponese } = require("../utils/validate")
const { listToTree,handleReg } = require("../utils/tools")
const PermissionController = require("./PermissionController")



const MenuController = {
    find: async (req, res) => {
        // 查询目录和菜单
        const result = await MenuServices.find(handleReg(req.query,["name", "status", "link", "type"]))
        let permissionParentids = []
        for (let i = 0; i < result.length; i++) {
           if (result[i].type === "menu") {
                permissionParentids.push(result[i]._id)
           }
        }
         // 查询权限字符
        let permissionResult = await PermissionController.find(permissionParentids)
        const data = listToTree(JSON.parse(JSON.stringify([...result,...permissionResult])))
        successResponese({
            res,
            msg: "查询成功!",
            data,
        })
    },
    append: async (req, res) => {
        const { type } = req.body
        let v
        // 校验参数
        type === "directory" ?
            v = await validate(MenuSchema.Directory, req.body) :
            type === "menu" ?
                v = await validate(MenuSchema.Menu, req.body) : type === "button" ?
                    v = await validate(MenuSchema.Button, req.body) : errorResponese({
                        res,
                        msg: "type类型不是规定格式"
                    })
        if (v) return errorResponese({ res: res, msg: v })

        if (req.body.type === "button") {
            // 单独处理button
             PermissionController.append(res,req.body)
             return
        }
        let result = await MenuServices.append(req.body)

        if (result) return successResponese({
            res,
            msg: "创建成功"
        })

        errorResponese({
            res,
            msg: "创建失败"
        })
    },
    update: async (req, res) => {
        const { type } = req.body
        let v
        // 校验参数
        type === "directory" ?
            v = await validate(MenuSchema.Directory, req.body) :
            type === "menu" ?
                v = await validate(MenuSchema.Menu, req.body) : type === "button" ?
                    v = await validate(MenuSchema.Button, req.body) : errorResponese({
                        res,
                        msg: "type类型不是规定格式"
                    })
        if (v) return errorResponese({ res: res, msg: v })
        if (req.body.type === "button") {
            // 单独处理button
            return
        }
        let result = await MenuServices.update(req.body)
        if (!result) return errorResponese({ res, msg: "修改用户失败" })
        successResponese({
            res,
            msg: "修改用户成功"
        })
    },
    remove: async (req, res) => {
        let data
        try {
             // 删除当前菜单需要连带删除其子级菜单
            const  removeChildren =  async (id) => {
               let result = await MenuServices.find({parentid:id})
               if (result.length === 0) {
                 await MenuServices.remove(id)
               } else {
                    for (let i = 0; i < result.length; i++) {
                        const data = result[i];
                        removeChildren(data._id)
                    }
               }
            }
            removeChildren(req.params.id)
            data = await MenuServices.remove(req.params.id)
        } catch (error) {
           return errorResponese({
                res,
                msg:error
            })
        }
        if (!data) return errorResponese({ res: res, msg: "删除菜单失败" });

        successResponese({ res: res, msg: "删除菜单成功" })
    },
    sort: () => { },
}

module.exports = MenuController